package com.macllvm.whale;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Message;
import android.widget.ProgressBar;
import android.os.AsyncTask;
import android.util.Log;
import com.macllvm.whale.ui.MainActivity;
import com.macllvm.whale.ui.dialog.CustomDialog;
import com.macllvm.whale.utils.NetworkTools;

public  class SplashActivity extends Activity {
    private static final String TAG = "SplashActivity";
    private WhaleApplication mApp;
    private Thread mCheckThread;
    private CustomDialog mAlertDialog;
    private Handler mHandler;
    private ProgressBar mProgressabr;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        mProgressabr = (ProgressBar)findViewById(R.id.progressbar);
        mAlertDialog = new CustomDialog(SplashActivity.this);
        mAlertDialog.setCancelable(false);
        mAlertDialog.setButtonOkCallback(new CustomDialog.IBtnOKCallback() {
            public void callback() {
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });


        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case Event.EVENT_WIFI_IS_NOT_OK:
                        Log.v(TAG, "wifi is not good....");
                        mAlertDialog.setMessage(getString(R.string.tip_wifi_closed));
                        mAlertDialog.show();
                        break;
                    case Event.EVENT_WIFI_IS_OK:
                        Log.v(TAG, "wifi is good....");
                        Intent intent = new Intent(SplashActivity.this,  MainActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                }
            }
        };


        mApp = (WhaleApplication)getApplication();


    }

    @Override
    public void onResume() {
        super.onResume();
        CheckTask task = new CheckTask();
        task.execute();
    }

    @Override
    public void onBackPressed(){
        return;
    }


//    class CheckThread implements Runnable {
//        //private WhaleApplication app;
//
//        //public CheckThread(WhaleApplication app) {
//        //    this.app = app;
//        //}
//
//        @Override
//        public void run() {
//            try {
//                Thread.sleep(500);
//                mApp.checkStoragePath();
//                mApp.loadSettings();
//
//            } catch (Exception e) {
//                Log.e(TAG, "check thread run exception");
//                e.printStackTrace();
//            }
//
//            if(mApp.wifiIsAvailable()) {
//                mHandler.sendEmptyMessage(Event.EVENT_WIFI_IS_OK);
//            }else{
//                mHandler.sendEmptyMessage(Event.EVENT_WIFI_IS_NOT_OK);
//            }
//        }
//    }

    private class CheckTask extends AsyncTask<Void, Integer, Void> {

        private boolean wifiIsAvailable = false;

        @Override
        protected void onPreExecute() {

        }


        @Override
        protected Void doInBackground(Void... params) {
            try {
                mApp.checkStoragePath();
                publishProgress(30);
                Thread.sleep(500);
                mApp.loadSettings();
                publishProgress(60);
                wifiIsAvailable = mApp.wifiIsAvailable();
                publishProgress(100);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onProgressUpdate(Integer... progresses) {
            mProgressabr.setProgress(progresses[0]);
        }


        @Override
        protected void onPostExecute(Void result) {
            if(wifiIsAvailable) {
                mApp.getSystemSettings().setLocalIP(NetworkTools.getLocalIP());
                mHandler.sendEmptyMessage(Event.EVENT_WIFI_IS_OK);
                Log.v(TAG, "wifi is connected.....");
            }else{
                mHandler.sendEmptyMessage(Event.EVENT_WIFI_IS_NOT_OK);
                Log.v(TAG, "wifi is not connected.....");
            }
        }


        @Override
        protected void onCancelled() {

        }
    }
} 
